/*
 * @Author: 飞越疯人院 353362604@qq.com
 * @Date: 2022-05-24 08:36:51
 * @LastEditors: 飞越疯人院 353362604@qq.com
 * @LastEditTime: 2023-01-05 17:23:36
 * @FilePath: \reconfiguration-administration\src\Common\Mars3D\RenderElements.jsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { ElementStyleData } from './Mars3Config'
const { mars3d } = window

export const RenderElements = (Map, Gra, data, status, clip) => {
    data.forEach(item => {
        const { storageType, elementType, styleName, elementUuid, storagePath, elementDetails, isEnabled } = item
        if (storageType === 1) {
            if (!Map.getLayer(elementUuid)) {
                const { color } = elementDetails[0]
                Map.addLayer(
                    new mars3d.layer.KmlLayer({
                        id: elementUuid,
                        url: storagePath,
                        symbol: ElementStyleData[elementType].type(color || '#262626')[styleName].style,
                        show: isEnabled,
                    }),
                )
            }
        } else {
            if (elementDetails && elementDetails.length !== 0) {
                elementDetails.forEach(key => {
                    const {
                        elementDetailUuid,
                        isEnabled,
                        position,
                        tableName,
                        textState,
                        modelUrl,
                        scale,
                        heading,
                        closure,
                        radius,
                        dimensions,
                        speed,
                        rotate,
                        slices,
                        angle,
                        color,
                        sortId,
                    } = key
                    if (Gra.graphics.findIndex(v => v.id === elementDetailUuid) === -1) {
                        switch (elementType) {
                            // 挖坑
                            case 'TerrainClip':
                                if (status) {
                                    const TerrainList = { 摩天岭隧道: 493, 丰林隧道: 310, 峰岩山隧道: 330 }
                                    position.forEach(key => {
                                        key[2] = TerrainList[tableName]
                                    })
                                    clip.addArea(position)
                                }
                                break
                            // 文字
                            case 'label':
                                Gra.addGraphic(
                                    new mars3d.graphic.LabelEntity({
                                        id: elementDetailUuid,
                                        position: position,
                                        style: ElementStyleData[elementType].type(tableName, color)[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 模型
                            case 'model':
                                Gra.addGraphic(
                                    new mars3d.graphic.ModelEntity({
                                        id: elementDetailUuid,
                                        position: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            modelUrl,
                                            scale,
                                            heading,
                                            Number(textState),
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 点
                            case 'point':
                                Gra.addGraphic(
                                    new mars3d.graphic.PointEntity({
                                        id: elementDetailUuid,
                                        position: position,
                                        style: ElementStyleData[elementType].type(tableName, Number(textState), color)[
                                            styleName
                                        ].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 图标点
                            case 'billboard':
                                Gra.addGraphic(
                                    new mars3d.graphic.BillboardEntity({
                                        id: elementDetailUuid,
                                        position: position,
                                        style: ElementStyleData[elementType].type(tableName, Number(textState), color)[
                                            styleName
                                        ].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 线
                            case 'polyline':
                                Gra.addGraphic(
                                    new mars3d.graphic.PolylineEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            closure,
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 曲线
                            case 'curve':
                                Gra.addGraphic(
                                    new mars3d.graphic.CurveEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            closure,
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 管道线
                            case 'polylineVolume':
                                Gra.addGraphic(
                                    new mars3d.graphic.PolylineVolumeEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            radius,
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 墙
                            case 'wall':
                                Gra.addGraphic(
                                    new mars3d.graphic.WallEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            closure,
                                            Number(dimensions),
                                            speed,
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 矩形
                            case 'rectangle':
                                Gra.addGraphic(
                                    new mars3d.graphic.RectangleEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            Number(rotate),
                                            Number(dimensions),
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 多边形
                            case 'polygon':
                                if (tableName === '开挖') clip.addArea(position)
                                else {
                                    Gra.addGraphic(
                                        new mars3d.graphic.PolygonEntity({
                                            id: elementDetailUuid,
                                            positions: position,
                                            style: ElementStyleData[elementType].type(
                                                tableName,
                                                Number(textState),
                                                Number(rotate),
                                                Number(dimensions),
                                                color,
                                            )[styleName].style,
                                            show: isEnabled,
                                        }),
                                    )
                                }
                                break
                            // 正多边形
                            case 'regular':
                                Gra.addGraphic(
                                    new mars3d.graphic.Regular({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            Number(dimensions),
                                            Number(slices),
                                            Number(radius),
                                            Number(rotate),
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 圆形
                            case 'circle':
                                Gra.addGraphic(
                                    new mars3d.graphic.CircleEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            Number(radius),
                                            Number(dimensions),
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 椭圆
                            case 'ellipse':
                                Gra.addGraphic(
                                    new mars3d.graphic.CircleEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            radius,
                                            Number(rotate),
                                            Number(dimensions),
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 扇形
                            case 'sector':
                                Gra.addGraphic(
                                    new mars3d.graphic.Sector({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            Number(radius),
                                            JSON.stringify(angle),
                                            Number(dimensions),
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 球
                            case 'ellipsoid':
                                Gra.addGraphic(
                                    new mars3d.graphic.EllipsoidEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            radius,
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // 圆锥
                            case 'cylinder':
                                Gra.addGraphic(
                                    new mars3d.graphic.CylinderEntity({
                                        id: elementDetailUuid,
                                        positions: position,
                                        style: ElementStyleData[elementType].type(
                                            tableName,
                                            Number(textState),
                                            radius,
                                            color,
                                        )[styleName].style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            // div
                            case 'div':
                                console.log(color)
                                Gra.addGraphic(
                                    new mars3d.graphic.DivGraphic({
                                        id: elementDetailUuid,
                                        name: tableName,
                                        position: position,
                                        hasZIndex: true,
                                        style: ElementStyleData[elementType].type(tableName, color, sortId)[styleName]
                                            .style,
                                        show: isEnabled,
                                    }),
                                )
                                break
                            default:
                                return console.log('未知元素,无法加载')
                        }
                    }
                })
            }
        }
    })
}

export const RenderFiles = (Map, data, additional) => {
    data.forEach(
        ({ scenesDetailsUuid, tableName, storagePath, scale, isEnabled, longitude, latitude, altitude, rotate }) => {
            if (!Map.getLayer(scenesDetailsUuid)) {
                const TilesetLayer = new mars3d.layer.TilesetLayer({
                    id: scenesDetailsUuid,
                    name: tableName,
                    url: storagePath,
                    maximumScreenSpaceError: 1,
                    maximumMemoryUsage: 1024,
                    skipLevelOfDetail: true,
                    preferLeaves: true,
                    dynamicScreenSpaceError: true,
                    cullWithChildrenBounds: false,
                    scale: scale || 1,
                    show: Number(isEnabled),
                    position: { lng: Number(longitude), lat: Number(latitude), alt: Number(altitude) },
                    rotation: rotate,
                })
                Map.addLayer(TilesetLayer).then(res => {
                    if (additional && additional?.findIndex(v => v.tableName === `${tableName}压平`) !== -1) {
                        const { elementDetails } = additional?.find(v => v.tableName === `${tableName}压平`)
                        elementDetails.forEach(({ position }) => TilesetLayer.clip.addArea(position))
                    }
                })
            }
        },
    )

    Map.getLayers().forEach(item => {
        if (item.id === '6be5e21f-ba45-486c-bb2e-6a9595eb8106') {
            let lay = new mars3d.thing.TilesetFlat({
                layer: item,
                area: [
                    {
                        positions: [
                            [127.028049, 44.94159, 103.3],
                            [127.028135, 44.941191, 102.7],
                            [127.028589, 44.941207, 102.7],
                            [127.02872, 44.940827, 102.7],
                            [127.030531, 44.941084, 102.7],
                            [127.030443, 44.941501, 102.7],
                            [127.031483, 44.941631, 102.7],
                            [127.031383, 44.942018, 102.7],
                        ],
                    },
                ],
                old: true,
            })
            Map.addThing(lay)
        }
        //  水库西
        if (
            item.id === 'd9e63ceb-cafb-4dcd-8ebf-4e450884c3a8' ||
            item.id === '06efb3e7-98ef-4f60-8cb2-340fbb3174c2' ||
            item.id === '247917bc-a9bd-4835-8364-f415842ee953' ||
            item.id === 'e300aa1e-2231-4f77-a5b6-8ec7d9783f31'
        ) {
            const [{ elementDetails }] = additional
            const { position } = elementDetails.find(
                v => v.elementDetailUuid === 'acc1dda9-bab1-4094-8a53-7ea1ddd219f8',
            )
            let lay = new mars3d.thing.TilesetFlat({
                layer: item,
                area: [{ positions: position }],
                old: true,
            })
            Map.addThing(lay)
        }
    })
}
